// app/controller/home.js
const Controller = require('../core/base_controller');
class GroupController extends Controller {
  async getGroupList() {
    const results = await this.app.mysql.select('t_group');
    this.ctx.body = {
      meta: {
        msg: "获取成功",
        status: 200,
      },
      data: {
        list: results.map(item=>{
          return{
            ...item,
            state:item.state =="关闭"?false:true
          }
        }),
      }
    };
  }
  async groupDetail() {  //参团详情
    const {id} = this.ctx.request.body;
    console.log(this.ctx.request.body);
    if(!id) return this.ctx.body = {meta: {msg: "必传项未传", status: 203,},};
    const sql = `select * from t_group 
          left join t_group_join on t_group.id=t_group_join.group_id
          left join t_client_user on t_client_user.c_id = t_group_join.client_id
          where  t_group.id = ${id}
          `
    const detail = await this.app.mysql.select('t_group', { // 搜索 t_group 表
      where: { id: id}, // WHERE 条件
      columns: ['img', 'title','address','time','state','teamcontext',"id"], // 要查询的表字段
    });
    const results = await this.app.mysql.query(sql);
    const newList = results.map(item=>{
      const {c_avatar,c_phone,c_nickname,c_id} = item
      return {
        c_avatar,c_phone,c_nickname,c_id
      }
    })
    this.ctx.body = {
      meta: {
        msg: "获取成功",
        status: 200,
      },
      data: {
        detail:{
          ...detail[0],
          peopleNum:newList.length
        },
        joinUserList: newList,
      }
    };
  }
  async changeGroupState() { //切换团购状态
    const {id,state=true} = this.ctx.request.body;
    if(!id) return this.ctx.body = {meta: {msg: "必传项未传", status: 203,},};
    const result = await this.app.mysql.update('t_group', {
      id:id,state:state==true?"开启":"关闭"
    });
    if(result.affectedRows !==1) return this.ctx.body = {meta: {msg: "切换失败", status: 203,},};
    this.ctx.body = {meta: {msg: "切换成功", status: 200,},};
  }
  async delGroupUser(){ //删除参团用户
    const {c_id,group_id} = this.ctx.request.body; 
    console.log(c_id,group_id);
    if(!(c_id &&group_id)) return this.ctx.body = {meta: {msg: "必传项未传", status: 203,},};
    const result = await this.app.mysql.delete('t_group_join', {
      client_id: c_id,group_id:group_id
    });
    if(result.affectedRows !==1) return this.ctx.body = {meta: {msg: "删除失败", status: 203,},};
    this.ctx.body = {meta: {msg: "删除成功", status: 200,},};
  }
  async upDataGroup() {
    const [files,fields] = await this.multiUpload();
    console.log(files,fields);
    const {content,address,time,endTime='用户未设置结束日期',teamcontext,} = fields;
    const result = await this.app.mysql.insert('t_group', {
      img:files.toString(),
      title,
      address,
      peopleNum:0,
      state:"开启",
      time:time + '-' + endTime,
      teamcontext,
      uid:1,  //待加入jwt后 需换成经纪人id
     });
     if(result.affectedRows === 1){
      this.ctx.body = {
        meta:{
          status:200,
          msg:"创建团购成功"
        },
      }
     } else {
      this.ctx.body = {
        meta:{
          status:203,
          msg:"创建团购失败"
        },
      }
     }
 
  }

}

module.exports = GroupController;